const a = require("./tree");

//运用递归和迭代两种方式实现二叉树的前序遍历
var preOrderTraversal = (root) => {
    let res = [];
    let preOrderTraversal = (node) => {
        if(node){
            res.push(node.value);
            preOrderTraversal(node.left);
            preOrderTraversal(node.right);
        }
    };
    preOrderTraversal(root);
    return res;
};

var preOrderTraversal2 = (root) =>{
    let res = [];
    let stack = [];
    if(root) stack.push(root);
    while(stack.length){
        let node = stack.pop();
        res.push(node.value);
        if(node.right != null){
            stack.push(node.right);
        }
        if(node.left !== null){
            stack.push(node.left);
        }
    }
    return res;
};

console.log(preOrderTraversal(a));
console.log(preOrderTraversal2(a));

